let fs = require('fs')

function getWords(file) {
    let res = fs.readFileSync(file)
    let words = res.toString('utf8').split(/\s/).filter(w => {
        return w
    })
    return words
}

function getMaxcount(obj) {
    let items = Object.keys(obj).map(function(key) {
        return [key, obj[key]];
    })
    items.sort(function(first, second) {
        return second[1] - first[1];
    })
    return items[0]
}

function frequencyCounter(file) {
    let words = getWords(file)
    let dict = {}
    words.forEach(w => {
        if (dict[w]) {
            dict[w] += 1
        } else {
            dict[w] = 1
        }
    })
    console.dir(getMaxcount(dict))
}

frequencyCounter('tale.txt')
